import { BaseMaterialService } from './material.service';
import { Body, Controller, Post, Get, Res, Headers, Req } from '@nestjs/common';
import { ApiOkResponse, ApiOperation, ApiSecurity, ApiTags } from '@nestjs/swagger';
import { ADMIN_PREFIX } from '../../admin.constants';
import { AdminUser } from '../../core/decorators/admin-user.decorator';
import i18n from '@main/i18n';
import { format } from 'date-fns';
import { FastifyRequest } from 'fastify';

@ApiSecurity(ADMIN_PREFIX)
@ApiTags('产品管理模块')
@Controller('material')
export class BaseMaterialController {
  constructor(private baseMaterialService: BaseMaterialService) {}

  @ApiOperation({ summary: '获取产品列表' })
  @ApiOkResponse()
  @Get('list')
  async list() {
    return await this.baseMaterialService.list();
  }

  @ApiOperation({ summary: '分页获取获取产品列表' })
  @ApiOkResponse()
  @Post('page')
  async page(@Body() dto) {
    const [list, total] = await this.baseMaterialService.page(dto);

    return {
      list,
      pagination: {
        total,
        pageNum: dto.pageNum,
        pageSize: dto.pageSize,
      },
    };
  }

  @ApiOperation({ summary: '添加产品' })
  @ApiOkResponse()
  @Post('add')
  async add(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.baseMaterialService.isExistKey(dto);

    await this.baseMaterialService.add(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '删除产品' })
  @ApiOkResponse()
  @Post('delete')
  async delete(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;

    await this.baseMaterialService.delete(dto, user, mac, ip, syncHostSqlite);
  }

  @ApiOperation({ summary: '获取产品字段列表' })
  @ApiOkResponse()
  @Get('cloums')
  async cloums() {
    return await this.baseMaterialService.getColumnsList();
  }

  @ApiOperation({ summary: '获取产品保存下发参数' })
  @ApiOkResponse()
  @Post('getParameterByMaterialVersion')
  async getParameterByMaterialVersion(@Body() dto) {
    return await this.baseMaterialService.getParameterByMaterialVersion(dto);
  }

  @ApiOperation({ summary: '保存产品下发单元参数' })
  @ApiOkResponse()
  @Post('saveParameterByMaterialVersion')
  async saveParameterByMaterialVersion(@Body() dto, @AdminUser() user, @Req() req: FastifyRequest) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.baseMaterialService.saveParameterByMaterialVersion(
      dto,
      user,
      mac,
      ip,
      syncHostSqlite
    );
  }

  @ApiOperation({ summary: '获取产品保存下发单元参数' })
  @ApiOkResponse()
  @Post('getParameterUnitByMaterialVersion')
  async getParameterUnitByMaterialVersion(@Body() dto) {
    return await this.baseMaterialService.getParameterUnitByMaterialVersion(dto);
  }

  @ApiOperation({ summary: '保存产品下发参数' })
  @ApiOkResponse()
  @Post('saveParameterUnitByMaterialVersion')
  async saveParameterUnitByMaterialVersion(
    @Body() dto,
    @AdminUser() user,
    @Req() req: FastifyRequest
  ) {
    const { mac = '', ip = '', synchostsqlite: syncHostSqlite = '' } = req.headers;
    await this.baseMaterialService.saveParameterUnitByMaterialVersion(
      dto,
      user,
      mac,
      ip,
      syncHostSqlite
    );
  }

  @ApiOperation({ summary: '导出产品资料' })
  @ApiOkResponse()
  @Post('export')
  async export(@Body() dto, @Res() res, @Headers('language') languageType) {
    const buffer = await this.baseMaterialService.export(dto, languageType);

    const langObj = i18n[languageType]['common']['table'];

    res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

    res.header(
      'Content-Disposition',
      `attachment; filename=${encodeURIComponent(
        langObj['material'] + format(new Date(), 'yyyyMMddHHmmss') + '.xlsx'
      )}`
    );

    res.header('Access-Control-Expose-Headers', 'Content-Disposition');

    res.send(buffer);
  }
}
